//
//  JMTencent.m
//  JMLibrary
//
//  Created by zhengxianda on 2018/7/24.
//

#import "JMTencent.h"

@implementation JMTencent

- (void)registerApp:(NSString *)appId{
    self.auth = [[TencentOAuth alloc] initWithAppId:appId andDelegate:self];
}

- (BOOL)handleOpenURL:(NSURL *)url{
    NSArray<NSString *> *infoList = [url.absoluteString componentsSeparatedByString:@"&"];
    for (NSString *info in infoList) {
        if ([info containsString:@"error="]) {
            int code = [[info substringFromIndex:6] intValue];
            [self onResp:code];
            break;
        }
    }
    return [TencentOAuth HandleOpenURL:url];
}

- (void)shareToChannel:(JMShareChannel)channel messageObject:(JMShareMessageObject *)messageObject handler:(JMShareRequestHandler)handler{
    [super shareToChannel:channel messageObject:messageObject handler:handler];
    
    QQApiSendResultCode sent = EQQAPISENDFAILD;
    switch (channel) {
        case JMShareChannelTencentQQ:
            sent = [self shareToQQWithMessageObject:messageObject handler:handler];
            break;
        case JMShareChannelTencentQQZone:
            sent = [self shareToQQZoneWithMessageObject:messageObject handler:handler];
            break;
        default:
            break;
    }
    
    BOOL result = sent == EQQAPISENDSUCESS;
    if (!result) {
        [self callHandlerWithCode:EQQAPISENDFAILD];
    }
}

- (QQApiSendResultCode)shareToQQWithMessageObject:(JMShareMessageObject *)messageObject handler:(JMShareRequestHandler)handler{
    QQApiObject *object;
    if (messageObject.musicDataURL && messageObject.musicPageURL) {
        object = [QQApiURLObject objectWithURL:messageObject.musicPageURL
                                         title:messageObject.title
                                   description:messageObject.descriptions
                              previewImageData:messageObject.thumbImage
                             targetContentType:QQApiURLTargetTypeAudio];
    }else if (messageObject.webPageURL) {
        object = [QQApiURLObject objectWithURL:messageObject.webPageURL
                                         title:messageObject.title
                                   description:messageObject.descriptions
                              previewImageData:messageObject.thumbImage
                             targetContentType:QQApiURLTargetTypeNews];
    }else if (messageObject.shareImage) {
        object = [QQApiImageObject objectWithData:messageObject.shareImage
                                 previewImageData:messageObject.thumbImage
                                            title:messageObject.title
                                      description:messageObject.descriptions
                                   imageDataArray:@[]];
    }else{
        object = [QQApiTextObject objectWithText:messageObject.text];
    }
    
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object];
    return [QQApiInterface sendReq:req];
}

- (QQApiSendResultCode)shareToQQZoneWithMessageObject:(JMShareMessageObject *)messageObject handler:(JMShareRequestHandler)handler{
    QQApiObject *object;
    if (messageObject.musicDataURL && messageObject.musicPageURL) {
        object = [QQApiURLObject objectWithURL:messageObject.musicPageURL
                                         title:messageObject.title
                                   description:messageObject.descriptions
                              previewImageData:messageObject.thumbImage
                             targetContentType:QQApiURLTargetTypeAudio];
    }else if (messageObject.webPageURL) {
        object = [QQApiURLObject objectWithURL:messageObject.webPageURL
                                         title:messageObject.title
                                   description:messageObject.descriptions
                              previewImageData:messageObject.thumbImage
                             targetContentType:QQApiURLTargetTypeNews];
    }else if (messageObject.shareImage) {
        object = [QQApiImageArrayForQZoneObject objectWithimageDataArray:@[messageObject.shareImage]
                                                                   title:messageObject.title
                                                                  extMap:@{}];
    }else{
        object = [QQApiImageArrayForQZoneObject objectWithimageDataArray:@[]
                                                                   title:messageObject.text
                                                                  extMap:@{}];
    }
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object];
    return [QQApiInterface SendReqToQZone:req];
}

- (NSString*)errorMsgWithCode:(int)code{
    NSString *msg = @"";
    switch(code){
        case EQQAPISENDSUCESS:
            msg = @"";
            break;
        case EQQAPIQQNOTINSTALLED:
            msg = @"QQ未安装";
            break;
        case EQQAPIQQNOTSUPPORTAPI:
            msg = @"QQ api不支持";
            break;
        case EQQAPIMESSAGETYPEINVALID:
            msg = @"";
            break;
        case EQQAPIMESSAGECONTENTNULL:
            msg = @"";
            break;
        case EQQAPIMESSAGECONTENTINVALID:
            msg = @"";
            break;
        case EQQAPIAPPNOTREGISTED:
            msg = @"";
            break;
        case EQQAPIAPPSHAREASYNC:
            msg = @"";
            break;
        case EQQAPIQQNOTSUPPORTAPI_WITH_ERRORSHOW:
            msg = @"QQ api不支持 && SDK显示error提示（已废弃）";
            break;
        case EQQAPIMESSAGEARKCONTENTNULL:
            msg = @"ark内容为空";
            break;
        case EQQAPISENDFAILD:
            msg = @"发送失败";
            break;
        case EQQAPISHAREDESTUNKNOWN:
            msg = @"未指定分享到QQ或TIM";
            break;
        case EQQAPITIMSENDFAILD:
            msg = @"发送失败";
            break;
        case EQQAPITIMNOTINSTALLED:
            msg = @"TIM未安装";
            break;
        case EQQAPITIMNOTSUPPORTAPI:
            msg = @"// TIM api不支持";
            break;
        case EQQAPIQZONENOTSUPPORTTEXT:
            msg = @"qzone分享不支持text类型分享";
            break;
        case EQQAPIQZONENOTSUPPORTIMAGE:
            msg = @"qzone分享不支持image类型分享";
            break;
        case EQQAPIVERSIONNEEDUPDATE:
            msg = @"当前QQ版本太低，需要更新至新版本才可以支持";
            break;
        case ETIMAPIVERSIONNEEDUPDATE:
            msg = @"当前TIM版本太低，需要更新至新版本才可以支持";
            break;
    }
    return msg;
}

- (void)tencentDidLogin {
    
}

- (void)tencentDidNotLogin:(BOOL)cancelled {
    
}

- (void)tencentDidNotNetWork {
    
}

- (void)onResp:(int)code{
    [self callHandlerWithCode:code];
}

@end
